package com.gxa.weixing.pc.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.weixing.core.common.ExceptionCode;
import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.Menu;
import com.gxa.weixing.core.pojo.Role;
import com.gxa.weixing.pc.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleController
 * @Author: ws
 * @date 2022/1/6 14:18
 */
@RestController
@Api(tags = "权限组(角色)相关的api")
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;


    @ApiOperation(value = "分页全查询")
    @GetMapping("/getAll")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数"),
            @ApiImplicitParam(name = "size",value = "每页记录数"),
    })
    public Map getAll(@RequestParam("page") Integer page, @RequestParam("size") Integer size) throws BusinessException {
        // 参数校验
        if (StringUtils.isEmpty(page) || page <0){
            throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ERROR);
        }
        if (StringUtils.isEmpty(size) || size <0){
            throw new BusinessException(ExceptionCode.PAGE_OR_SIZE_ERROR);
        }

        PageInfo<Role> info = roleService.getAll(page, size);
        long total = info.getTotal();
        List list = info.getList();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(list);
        jsonResult.put("total",total);
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "添加接口")
    @PostMapping("/add")
    public Map add(@RequestBody Role role) throws BusinessException {
        System.out.println(role);
        // 校验
        if (StringUtils.isEmpty(role.getRoleName())) {
            throw new BusinessException(ExceptionCode.NAME_NOTNULL);
        }

        // 修改时间
        role.setRoleUpdateTime(new Date());
        // 默认启用
        role.setRoleState(1L);
        // 添加
        Role insert = roleService.insert(role);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        jsonResult.setData(insert);
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "修改接口")
    @PostMapping("/update")
    public Map update(@RequestBody Role role) throws BusinessException {
        // 校验
        if (StringUtils.isEmpty(role.getRoleName())) {
            throw new BusinessException(ExceptionCode.NAME_NOTNULL);
        }
        if (StringUtils.isEmpty(role.getRoleId())) {
            throw new BusinessException(ExceptionCode.ID_NOTNULL);
        }
        // 修改时间
        role.setRoleUpdateTime(new Date());
        // 更新
        Integer integer = roleService.updateById(role);
        JsonResult jsonResult = new JsonResult();

        if (integer == 1) {
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("300");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "删除接口")
    @PostMapping("/delete")
    @ApiImplicitParam(name = "id",value = "权限组(角色)编号",dataType = "long",paramType = "query")
    public Map delete(@RequestParam("id") Long id) throws BusinessException {
        // 校验参数
        if(StringUtils.isEmpty(id)){
            throw new BusinessException(ExceptionCode.PARAM_NOTNULL);
        }

        Integer integer = roleService.delete(id);
        JsonResult jsonResult = new JsonResult();
        if (integer == 1) {
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("300");
            jsonResult.setMsg("删除失败");
        }
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "点击权限菜单编辑(渲染权限列表)")
    @GetMapping("/getMenus")
    @ApiImplicitParam(name = "roleId",value = "权限组(角色)的编号")
    public Map getMenus(@RequestParam("roleId") Long roleId) throws BusinessException {
        List<Menu> roleMenus = roleService.getMenus(roleId);
        List<Menu> menus = roleService.getMenus();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("roleMenus",roleMenus);
        jsonResult.put("menus",menus);
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "权限菜单编辑提交(给角色授权)")
    @PostMapping("/authorization")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "权限组(角色)的编号",paramType = "query",dataType = "Long"),
            @ApiImplicitParam(name = "menuIds",value = "权限菜单的编号数组(eg: 1,2,3,4,5)",paramType = "query",dataType = "Long[]"),
    })
    public Map authorization(@RequestParam("roleId") Long roleId,@RequestParam("menuIds") Long[] menuIds) throws BusinessException {
        /*System.out.println("权限菜单编辑提交(给角色授权)的接口参数：");
        System.out.println("roleId: "+roleId);
        System.out.println("menuIds: ");
        for (Long menuId : menuIds) {
            System.out.println(menuId);
        }*/
        // 参数校验
        if (StringUtils.isEmpty(roleId)){
            throw new BusinessException(ExceptionCode.ID_NOTNULL);
        }
        if (StringUtils.isEmpty(menuIds) || menuIds.length <= 0) {
            throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
        }
        // 调用方法
        Integer authorization = roleService.authorization(roleId, menuIds);
        // 打印影响行数
        System.out.println("操作影响行数："+authorization);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("授权成功");
        return jsonResult.getDataMap();
    }

}
